package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Syslog;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/syslog")
public class SyslogServlet extends BaseServlet {
    public  void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String str = request.getParameter("str");
        if(StringUtils.isNotBlank(str)){
            findByMethodName(request,response);
            return;
        }
        int page =1;
        int size=10;

        if (StringUtils.isNotBlank(request.getParameter("page"))) {
            page=Integer.parseInt(request.getParameter("page"));
        }

        if (StringUtils.isNotBlank(request.getParameter("size"))) {
           size=Integer.parseInt(request.getParameter("size"));
        }

        PageInfo info = syslogService.findAll(page, size);

        request.setAttribute("page",info);
        request.getRequestDispatcher("/WEB-INF/pages/store/syslog/syslog-list.jsp").forward(request,response);
    }




    public void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String[] ids = request.getParameterValues("ids");
        syslogService.deleteSelected(ids);
        response.sendRedirect(request.getContextPath()+"/store/syslog?operation=list");
    }

    public void downloadReport(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
       syslogService.getReport();
        response.sendRedirect(request.getContextPath()+"/store/syslog?operation=list");
    }

    public void findByMethodName(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        /*String methodName = request.getParameter("sousuo");
        List<Syslog> all = syslogService.findByMethodName(methodName);

        request.setAttribute("page",all);
        request.getRequestDispatcher("/WEB-INF/pages/system/syslog/syslog-list.jsp").forward(request,response);
*/
        String value = request.getParameter("str");
        String sizeStr = request.getParameter("size");
        int size=Integer.parseInt(sizeStr);
        System.out.println(value);
        //进入列表页获取数据
        int page = 1;

        if (StringUtils.isNotBlank(request.getParameter("page"))){
            page=Integer.parseInt(request.getParameter("page"));
        }
       /* if(StringUtils.isNotBlank(request.getParameter("size"))){
            size=Integer.parseInt(request.getParameter("size"));
        }*/
        PageInfo all = syslogService.findByMethodName(page, size, value);
        //将数据保存到指位置
        request.setAttribute("page",all);
        request.setAttribute("str",value);
        //跳转页面
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/store/syslog/syslog-list.jsp").forward(request,response);

    }
}
